using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
    /// <summary> 
    ///Author: nnamthach@gmail.com 
    /// <summary>
    public class BAOCAOTHUEDataAccess : Connection
    {
        #region Local Variable
        private string _strSPInsertName = "dbo.[procBAOCAOTHUE_add]";
        private string _strSPUpdateName = "dbo.[procBAOCAOTHUE_update]";
        private string _strSPDeleteName = "dbo.[procBAOCAOTHUE_delete]";
        private string _strSPGetName = "dbo.[procBAOCAOTHUE_get]";
        private string _strSPMaxBTName = "procBAOCAOTHUE_getmaxbt";
        private string _strSPGetAllName = "dbo.[procBAOCAOTHUE_getall]";
        private string _strSPGetPages = "dbo.[procBAOCAOTHUE_getpaged]";
        private string _strSPIsExist = "dbo.[procBAOCAOTHUE_isexist]";
        private string _strTableName = "[BAOCAOTHUE]";
        private string _strSPGetTransferOutName = "dbo.[procBAOCAOTHUE_gettransferout]";
        string _strSPGetCountName = "procBAOCAOTHUE_getcount";
        string _strSPGetByIndexName = "procBAOCAOTHUE_getindex";
        #endregion Local Variable

        #region Method
        public BAOCAOTHUEInfo Get(
        Int64 ID,
        ref string sErr)
        {
            BAOCAOTHUEInfo objEntr = new BAOCAOTHUEInfo();
            InitConnect();
            InitSPCommand(_strSPGetName);
            AddParameter(BAOCAOTHUEInfo.Field.ID.ToString(), ID);

            DataTable list = new DataTable();
            try
            {
                list = executeSelectSP(command);
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }

            if (list.Rows.Count > 0)
                objEntr = new BAOCAOTHUEInfo(list.Rows[0]);
            //if (dr != null) list = CBO.FillCollection(dr, ref list);
            if (sErr != "") ErrorLog.SetLog(sErr);
            return objEntr;
        }

        public DataTable GetAll(
        ref string sErr)
        {
            InitConnect();
            InitSPCommand(_strSPGetAllName);
            DataTable list = new DataTable();
            try
            {
                list = executeSelectSP();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }


            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
        public DataTable GetByPos(
        int pos, ref string sErr)
        {
            InitConnect();
            InitSPCommand(_strSPGetByIndexName);
            AddParameter("INX", pos);
            DataTable list = new DataTable();
            try
            {
                list = executeSelectSP();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }


            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
        public int GetCountRecord(
        ref string sErr)
        {
            int ret = -1;
            connect();
            InitSPCommand(_strSPGetCountName);

            try
            {
                //command.ExecuteNonQuery();
                object tmp = executeSPScalar();
                if (tmp != null && tmp != DBNull.Value)
                    ret = Convert.ToInt32(tmp);
                else
                    ret = 0;
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            if (sErr != "") ErrorLog.SetLog(sErr);

            return ret;
        }
        /// <summary>
        /// Return 1: Table is exist Identity Field
        /// Return 0: Table is not exist Identity Field
        /// Return -1: Erro
        /// </summary>
        /// <param name="tableName"></param>
        public Int32 Add(BAOCAOTHUEInfo objEntr, ref string sErr)
        {
            int ret = -1;
            connect();
            InitSPCommand(_strSPInsertName);
            AddParameter(BAOCAOTHUEInfo.Field.NGAYPS.ToString(), objEntr.NGAYPS);
            AddParameter(BAOCAOTHUEInfo.Field.KYHIEUHD.ToString(), objEntr.KYHIEUHD);
            AddParameter(BAOCAOTHUEInfo.Field.SOHD.ToString(), objEntr.SOHD);
            AddParameter(BAOCAOTHUEInfo.Field.TENKH.ToString(), objEntr.TENKH);
            AddParameter(BAOCAOTHUEInfo.Field.MSTHUE.ToString(), objEntr.MSTHUE);
            AddParameter(BAOCAOTHUEInfo.Field.HANGHOA.ToString(), objEntr.HANGHOA);
            AddParameter(BAOCAOTHUEInfo.Field.DOANHSO.ToString(), objEntr.DOANHSO);
            AddParameter(BAOCAOTHUEInfo.Field.THUESUAT.ToString(), objEntr.THUESUAT);
            AddParameter(BAOCAOTHUEInfo.Field.THUEGTGT.ToString(), objEntr.THUEGTGT);
            AddParameter(BAOCAOTHUEInfo.Field.GHICHU.ToString(), objEntr.GHICHU);
            AddParameter(BAOCAOTHUEInfo.Field.BANRA.ToString(), objEntr.BANRA);
            AddParameter(BAOCAOTHUEInfo.Field.DIACHI.ToString(), objEntr.DIACHI);
            AddParameter(BAOCAOTHUEInfo.Field.SOBT.ToString(), objEntr.SOBT);
            AddParameter(BAOCAOTHUEInfo.Field.EXP2HTKK.ToString(), objEntr.EXP2HTKK);
            AddParameter(BAOCAOTHUEInfo.Field.NGAYGD.ToString(), objEntr.NGAYGD);
            AddParameter(BAOCAOTHUEInfo.Field.CUSINFO.ToString(), objEntr.CUSINFO);
            AddParameter(BAOCAOTHUEInfo.Field.LOAIBT.ToString(), objEntr.LOAIBT);
            AddParameter(BAOCAOTHUEInfo.Field.CREATEBY.ToString(), objEntr.CREATEBY);
            AddParameter(BAOCAOTHUEInfo.Field.MODIFIEDBY.ToString(), objEntr.MODIFIEDBY);
            AddParameter(BAOCAOTHUEInfo.Field.CREATEDATE.ToString(), objEntr.CREATEDATE);
            AddParameter(BAOCAOTHUEInfo.Field.MODIFIEDDATE.ToString(), objEntr.MODIFIEDDATE);

            try
            {
                //command.ExecuteNonQuery();
                object tmp = executeSPScalar();
                if (tmp != null && tmp != DBNull.Value)
                    ret = Convert.ToInt32(tmp);
                else
                    ret = 0;
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            if (sErr != "") ErrorLog.SetLog(sErr);

            return ret;
        }

        public string Update(BAOCAOTHUEInfo objEntr)
        {
            connect();
            InitSPCommand(_strSPUpdateName);
            AddParameter(BAOCAOTHUEInfo.Field.ID.ToString(), objEntr.ID);
            AddParameter(BAOCAOTHUEInfo.Field.NGAYPS.ToString(), objEntr.NGAYPS);
            AddParameter(BAOCAOTHUEInfo.Field.KYHIEUHD.ToString(), objEntr.KYHIEUHD);
            AddParameter(BAOCAOTHUEInfo.Field.SOHD.ToString(), objEntr.SOHD);
            AddParameter(BAOCAOTHUEInfo.Field.TENKH.ToString(), objEntr.TENKH);
            AddParameter(BAOCAOTHUEInfo.Field.MSTHUE.ToString(), objEntr.MSTHUE);
            AddParameter(BAOCAOTHUEInfo.Field.HANGHOA.ToString(), objEntr.HANGHOA);
            AddParameter(BAOCAOTHUEInfo.Field.DOANHSO.ToString(), objEntr.DOANHSO);
            AddParameter(BAOCAOTHUEInfo.Field.THUESUAT.ToString(), objEntr.THUESUAT);
            AddParameter(BAOCAOTHUEInfo.Field.THUEGTGT.ToString(), objEntr.THUEGTGT);
            AddParameter(BAOCAOTHUEInfo.Field.GHICHU.ToString(), objEntr.GHICHU);
            AddParameter(BAOCAOTHUEInfo.Field.BANRA.ToString(), objEntr.BANRA);
            AddParameter(BAOCAOTHUEInfo.Field.DIACHI.ToString(), objEntr.DIACHI);
            AddParameter(BAOCAOTHUEInfo.Field.SOBT.ToString(), objEntr.SOBT);
            AddParameter(BAOCAOTHUEInfo.Field.EXP2HTKK.ToString(), objEntr.EXP2HTKK);
            AddParameter(BAOCAOTHUEInfo.Field.NGAYGD.ToString(), objEntr.NGAYGD);
            AddParameter(BAOCAOTHUEInfo.Field.CUSINFO.ToString(), objEntr.CUSINFO);
            AddParameter(BAOCAOTHUEInfo.Field.LOAIBT.ToString(), objEntr.LOAIBT);
            AddParameter(BAOCAOTHUEInfo.Field.CREATEBY.ToString(), objEntr.CREATEBY);
            AddParameter(BAOCAOTHUEInfo.Field.MODIFIEDBY.ToString(), objEntr.MODIFIEDBY);
            AddParameter(BAOCAOTHUEInfo.Field.CREATEDATE.ToString(), objEntr.CREATEDATE);
            AddParameter(BAOCAOTHUEInfo.Field.MODIFIEDDATE.ToString(), objEntr.MODIFIEDDATE);

            string sErr = "";
            try
            {
                excuteSPNonQuery();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            if (sErr != "") ErrorLog.SetLog(sErr);
            return sErr;
        }

        public string Delete(
        Int64 ID
        )
        {
            connect();
            InitSPCommand(_strSPDeleteName);
            AddParameter(BAOCAOTHUEInfo.Field.ID.ToString(), ID);

            string sErr = "";
            try
            {
                excuteSPNonQuery();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            if (sErr != "") ErrorLog.SetLog(sErr);
            return sErr;
        }

        public DataTableCollection Get_Page(BAOCAOTHUEInfo obj, string orderBy, int pageIndex, int pageSize, ref String sErr)
        {
            string whereClause = CreateWhereClause(obj);
            DataTableCollection dtList = null;
            InitConnect();
            InitSPCommand(_strSPGetPages);

            AddParameter("WhereClause", whereClause);
            AddParameter("OrderBy", orderBy);
            AddParameter("PageIndex", pageIndex);
            AddParameter("PageSize", pageSize);

            try
            {
                dtList = executeCollectSelectSP();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }

            if (sErr != "") ErrorLog.SetLog(sErr);
            return dtList;
        }

        public Boolean IsExist(
        Int64 ID
        )
        {
            connect();
            InitSPCommand(_strSPIsExist);
            AddParameter(BAOCAOTHUEInfo.Field.ID.ToString(), ID);

            string sErr = "";
            DataTable list = new DataTable();
            try
            {
                list = executeSelectSP();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            if (sErr != "") ErrorLog.SetLog(sErr);
            if (list.Rows.Count == 1)
                return true;
            return false;
        }

        private string CreateWhereClause(BAOCAOTHUEInfo obj)
        {
            String result = "";

            return result;
        }

        public DataTable Search(string columnName, string columnValue, string condition, string tableName, ref string sErr)
        {
            string query = "select * from " + _strTableName + " where " + columnName + " " + condition + " " + columnValue;
            DataTable list = new DataTable();
            InitConnect();
            try
            {
                list = executeSelectQuery(query);
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }

            //if (dr != null) list = CBO.FillCollection(dr, ref list);
            //    if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
        public DataTable GetTransferOut(string dtb, object from, object to, ref string sErr)
        {
            InitConnect();
            InitSPCommand(_strSPGetTransferOutName);
            AddParameter("DB", dtb);
            AddParameter("FROM", from);
            AddParameter("TO", to);
            DataTable list = new DataTable();
            try
            {
                list = executeSelectSP();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }


            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
        #endregion Method


        public DataTable GetAll(bool isBanRa, ref string sErr)
        {
            InitConnect();
            InitSPCommand(_strSPGetAllName);
            AddParameter(DTO.BAOCAOTHUEInfo.Field.BANRA.ToString(), isBanRa);
            DataTable list = new DataTable();
            try
            {
                list = executeSelectSP();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }


            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
    

        public string GetMaxID(int year)
        {
            string ret = "00001"; ;
            connect();
            InitSPCommand(_strSPMaxBTName);
            AddParameter("Year", year);
            string sErr = "";
            try
            {
                //command.ExecuteNonQuery();
                object tmp = executeSPScalar();
                if (tmp != null && tmp != DBNull.Value)
                    ret = tmp.ToString();
                else
                    ret = "";
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            if (sErr != "") ErrorLog.SetLog(sErr);

            return ret;
        }

        public DataTable GetAll(string loaibt, ref string sErr)
        {
            InitConnect();
            InitSPCommand(_strSPGetAllName);
            AddParameter(DTO.BAOCAOTHUEInfo.Field.LOAIBT.ToString(), loaibt);
            DataTable list = new DataTable();
            try
            {
                list = executeSelectSP();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }


            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
    }
}
